home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Nebula 1
/
Nebula One.iso
/
Internet
/
WWW
/
gform.1.1
/
send_to_printer.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-02-10
|
1KB
|
69 lines
#include <string.h>
/***
* Send buf to printer
*
* queue - The intended print queue.
* buf - The data to print.
*
* returns 0 on success
* -1 on failure.
*
***/
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
#include "config.h"
#ifndef PRINTCMD
#define PRINTCMD "/usr/bin/lpr"
#endif
#ifndef PRINTQ
#define PRINTQ "-P"
#endif
int send_to_printer(queue,buf)
char *queue, *buf;
{
int fd[2], pid;
FILE *f1;
char thequeue[256];
/* set child process STDIN to parent fd */
if (pipe(fd) <0)
return(-1);
if ((pid = fork()) < 0)
return(-1);
else if (pid >0) { /* parent */
close(fd[0]); /* close read end */
write(fd[1], buf, strlen(buf));
close(fd[1]);
if (waitpid(pid, NULL, 0) <0)
return(-1);
return(0);
} else { /* child */
close(fd[1]);
if (fd[0] != STDIN_FILENO) {
if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO)
return(-1);
close(fd[0]);
} /* invoke printer which expects STDIN */
if (queue!=NULL)
sprintf(thequeue,"%s%s",PRINTQ,queue);
else
thequeue[0]=0;
if (execl(PRINTCMD,PRINTCMD,thequeue, 0) <0)
return(-1);
close(fd[0]);
exit(0);
}
return(0);
}